package com.niiwoo.civet.trade.service.local.configure;

import com.niiwoo.civet.trade.dao.entity.ProjectLoanDeadlineConfig;
import com.niiwoo.civet.trade.dao.entity.RateConfig;
import com.niiwoo.civet.trade.dao.entity.TradeConfig;
import com.niiwoo.civet.trade.dao.mapper.ProjectLoanDeadlineConfigMapperExt;
import com.niiwoo.civet.trade.dao.mapper.RateConfigMapperExt;
import com.niiwoo.civet.trade.dao.mapper.TradeConfigMapperExt;
import com.niiwoo.civet.trade.enums.ProjectTypeEnum;
import com.niiwoo.civet.trade.enums.TradeConfigEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TradeConfigLocalService {

    @Autowired
    private TradeConfigMapperExt tradeConfigMapperExt;
    @Autowired
    private RateConfigMapperExt rateConfigMapperExt;

    @Autowired
    private ProjectLoanDeadlineConfigMapperExt projectLoanDeadlineConfigMapperExt;

    @Transactional(isolation = Isolation.READ_COMMITTED)
    public String selectTradeConfigByEmun(TradeConfigEnum configEnum){
        return tradeConfigMapperExt.selectTradeConfigByEmun(configEnum);
    }
    
	public String selectTradeConfigByEmun(TradeConfigEnum configEnum, String defaultValue) {
		String value = tradeConfigMapperExt.selectTradeConfigByEmun(configEnum);
		if (StringUtils.hasLength(value)) {
			return value;
		}
		return defaultValue;
	}    

    public Map<String, String> selectTradeConfigByEmuns(List<TradeConfigEnum> enumList) {
        Map<String, String> map =new HashMap<String,String>();
        if (enumList == null || enumList.size() == 0){
            return map;
        }
        List<TradeConfig> tradeConfigList = tradeConfigMapperExt.selectTradeConfigByEmuns(enumList);
        if (tradeConfigList == null || tradeConfigList.size() == 0){
            return map;
        }
        for (TradeConfig config : tradeConfigList){
            String key =config.getConfigKey();
            String value =config.getConfigValue();
            map.put(key,value);
        }
        return map;
    }

    public Map<String, String> selectTradeConfigByEmuns(TradeConfigEnum ... enumList){
        return this.selectTradeConfigByEmuns(Arrays.asList(enumList));
    }

    public String selectDeadlineByAmount(BigDecimal amount){
        ProjectLoanDeadlineConfig projectLoanDeadlineConfig = projectLoanDeadlineConfigMapperExt.selectByAmount(ProjectTypeEnum.PERSONAL_SPEEDLOAN,amount);

        if (projectLoanDeadlineConfig != null) {
            return projectLoanDeadlineConfig.getDeadline();
        }
        return null;
    }

    public Byte rateConfig(int loanTerm, Byte configType){
        RateConfig rateConfig = rateConfigMapperExt.queryRateConfigByDeadline(loanTerm,configType);
        if(rateConfig != null){
            return rateConfig.getRepaymentMode();
        }
        return 0;
    }
}
